package com.sy.controller.purchase;

import com.sy.bean.purchase.Purchase;
import com.sy.bean.purchase.PurchaseExcel;
import com.sy.bean.purchase.QueryPurchase;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.common.utils.BeanOfMap;
import com.sy.service.purchase.PurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "purchase=采购管理")
@RequestMapping("/api/purchase")
public class PurchaseController extends BaseController {

    @Autowired
    PurchaseService purchaseService;
    @ApiOperation("查询采购列表")
    @PostMapping("/query")
    public ResultBean query(@RequestBody QueryPurchase queryPurchase){
        PageBean bean=purchaseService.query(BeanOfMap.bom(queryPurchase));
        return success("查询采购列表",bean);
    }

    @ApiOperation("保存采购")
    @PostMapping("/save")
    public ResultBean save(@RequestBody Purchase purchase){
        int rs=purchaseService.save(purchase);
        return rs==1?success("保存成功"):fail("保存失败");
    }

    @ApiOperation("批量删除采购单")
    @DeleteMapping("/delete")
    public ResultBean delete(@RequestParam("ids") List<Integer> ids){
       int rs=purchaseService.batchDelete(ids);
       return rs>0?success("删除成功"):fail("删除失败");
    }

    @ApiOperation("获取采购单详情")
    @GetMapping("/get")
    public ResultBean get(int id){
        Purchase purchase=purchaseService.get(id);
        return success("获取采购单详情",purchase);
    }

    @ApiOperation("导出采购单")
    @PostMapping("/export")
    public ResultBean<PurchaseExcel> export(@RequestBody Map map){
        String url=purchaseService.export(map);
        return success("导出采购单",url);
    }
}
